package com.thermofisher.dsc.amanda.common.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.thermofisher.dsc.amanda.common.crud.Util;
import com.thermofisher.dsc.amanda.common.excel.model.Validate;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by wenjie.yang on 4/3/2019.
 */
@Slf4j
public class ExcelListener<T> extends AnalysisEventListener {


  public ExcelListener(Class listenerClazz) {
    TITLE.put("SKU_NBR","skuNbr");
    TITLE.put("SKU Desc","skuDesc");
    TITLE.put("2019LP CNY","lpCny");
    TITLE.put("AM Disc%","amDisc");
    TITLE.put("RM Disc%","rmDisc");
    TITLE.put("SD Disc%","sdDisc");
    TITLE.put("SKU Size","skuSize");
    this.listenerClazz = listenerClazz;
  }

  private List datas = new ArrayList<>();

  private Class listenerClazz;

  public static final LinkedHashMap<String,String> TITLE_FILED = new LinkedHashMap<>();
  public static final HashMap<String,String> TITLE = new HashMap<>();

  @Override
  public void invoke(Object object, AnalysisContext context) {
    Integer rowNum = context.getCurrentRowNum();
    if (listenerClazz.getName().indexOf("SkuPrice") != -1){
      List<String> list = (List<String>)object;
      if (TITLE_FILED.isEmpty()){
        list.stream().forEach(s->{
            TITLE_FILED.put(TITLE.get(s),null);
        });
      }
      if (rowNum>0){
        LinkedHashMap<String, String> linkedHashMap = this.copyMap();
        int i = 0;
        for (String key:linkedHashMap.keySet()){
          String vaule = list.get(i);
          if (isIntTitle(key)){
            try {
              vaule = Integer.valueOf(vaule).toString();
            }catch (NumberFormatException e){
              vaule = null;
            }
          }
          linkedHashMap.put(key,vaule==null?null:vaule.trim());
          i++;
        }
        linkedHashMap.remove(null);
        datas.add(linkedHashMap);
      }
    }else{
      if(((Validate)object).validate()){
        datas.add(object);
      }else {
        log.info("the {} num data is wrong, this object is {}", rowNum,object.toString());
      }
    }
  }

  private boolean isIntTitle(String key){
    if ("amDisc".equals(key) || "rmDisc".equals(key)|| "sdDisc".equals(key)|| "lpCny".equals(key)){
      return true;
    }
    return false;
  }

  private LinkedHashMap<String,String> copyMap(){
    LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>();
    TITLE_FILED.forEach((k,v)->linkedHashMap.put(k,null));
    return linkedHashMap;
  }


  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
    // datas.clear();//解析结束销毁不用的资源
  }

  private Class getThisClass(){
     return  Util.getActualArgumentType(this.getClass(), 0);
  }

  public void clear() {
    datas.clear();
  }
  public List<T> getDatas() {
    return datas;
  }
  public void setDatas(List<T> datas) {
    this.datas = datas;
  }
}
